<?php

!defined('IN_HDWIKI') && exit('Access Denied');

class channelmodel {

	var $db;
	var $base;

	function channelmodel(&$base) {
		$this->base = $base;
		$this->db = $base->db;
	}
	
	function  get_channel_by_id($id){
		return $this->db->fetch_first("SELECT * FROM ".DB_TABLEPRE."channel WHERE id=$id");
	}
	
	function get_all_list(){
		$channellist=array();
		$query=$this->db->query('SELECT * FROM '.DB_TABLEPRE.'channel ORDER BY displayorder,id DESC');
		while($channel=$this->db->fetch_array($query)){
			if(substr($channel['url'],0,7)!="http://"){
				$channel['url']='http://'.$channel['url'];
			}
			$channellist[]=$channel;
		}
		return $channellist;
	}

	function get_channel_list(){
		$channellist=array();
		$query=$this->db->query('SELECT * FROM '.DB_TABLEPRE.'channel where available=1 ORDER BY displayorder,id DESC');
		while($channel=$this->db->fetch_array($query)){
			if(substr($channel['url'],0,7)!="http://"){
				$channel['url']='http://'.$channel['url'];
			}
			$channellist[]=$channel;
		}
		return $channellist;
	}

	function add_channel($channel){
		return $this->db->query("INSERT INTO  ".DB_TABLEPRE."channel (name,url,available) VALUES ('$channel[name]','$channel[url]',$channel[available]) ");
	}
	
	function edit_channel($channel){
		return $this->db->query("UPDATE ".DB_TABLEPRE."channel SET name='$channel[name]',url='$channel[url]',available='$channel[available]' WHERE id=$channel[id] ");
	}
	
	function remove_channel($channelid){
		if(is_array($channelid)){
			$id=implode(",",$channelid);
		}else{
			$id=$channelid;
		}
		$this->db->query("DELETE FROM ".DB_TABLEPRE."channel WHERE id IN ($id)");
	}
	
	function updateorder($order){
		$count=count($order);
		for($i=0;$i<$count;$i++){
			$this->db->query("UPDATE ".DB_TABLEPRE."channel SET displayorder=$i WHERE id=".$order[$i]);
		}
	}
}
?>